#include "HttpResponse.h"
#include "Spark/Net/MsgBuffer.h"

const HttpResponse& HttpResponse::appendToBuffer(MsgBuffer* output) const
{
    char buf[32];
    snprintf(buf, sizeof buf, "HTTP/1.1 %d", static_cast<int>(status_));
    output->append(buf);
    output->append(message_);
    output->append("\r\n");
    if (close_)
    {
        output->append("Connection: close\r\n");
    }
    else
    {
        snprintf(buf, sizeof buf, "Content-Length: %zd\r\n", body_.size());
        output->append(buf);
        output->append("Connection: Keep-Alive\r\n");
    }

    for (const auto& header : headers_)
    {
        output->append(header.first);
        output->append(": ");
        output->append(header.second);
        output->append("\r\n");
    }

    output->append("\r\n");
    output->append(body_);
    return *this;
}
